<template>
    <div class="app-container">
        <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
            label-width="80px">
            <el-form-item label-width="150px" label="个人评优类别名称" prop="typeName">
                <el-input v-model="queryParams.typeName" placeholder="请输入个人评优类别名称" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="类别代码" prop="typeCode">
                <el-input v-model="queryParams.typeCode" placeholder="请输入类别代码" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label-width="150px" label="父级类别代码" prop="parentCode">
                <el-input v-model="queryParams.parentCode" placeholder="请输入父级类别代码" clearable
                    @keyup.enter.native="handleQuery" />
            </el-form-item>
            <el-form-item label="学年" prop="stuYearId">
                <el-select v-model="queryParams.stuYearId" clearable>
                    <el-option v-for="(v, i) in stuYearList" :key="i" :label="v.stuYearName" :value="v.id"></el-option>
                </el-select>

            </el-form-item>
            <el-form-item label="开始时间" prop="startTime">
                <el-date-picker clearable v-model="queryParams.startTime" type="date" value-format="yyyy-MM-dd"
                    placeholder="选择开始时间">
                </el-date-picker>
            </el-form-item>
            <el-form-item label="结束时间" prop="endTime">
                <el-date-picker clearable v-model="queryParams.endTime" type="date" value-format="yyyy-MM-dd"
                    placeholder="选择结束时间">
                </el-date-picker>
            </el-form-item>
            <el-form-item label="启用状态" prop="enableStatus">
                <el-select v-model="queryParams.enableStatus" placeholder="请选择启用状态" clearable>
                    <el-option v-for="dict in dict.type.all_status" :key="dict.value" :label="dict.label"
                        :value="dict.value" />
                </el-select>
            </el-form-item>
            <el-form-item label="开始状态" prop="startStatus">
                <el-select v-model="queryParams.startStatus" placeholder="请选择开始状态" clearable>
                    <el-option v-for="dict in dict.type.start_status" :key="dict.value" :label="dict.label"
                        :value="dict.value" />
                </el-select>
            </el-form-item>
            <el-form-item>
                <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
                <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
            </el-form-item>
        </el-form>

        <el-row :gutter="10" class="mb8">
            <el-col :span="1.5">
                <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
                    v-hasPermi="['comprehensive:type:add']">新增</el-button>
            </el-col>
            <el-col :span="1.5">
                <el-button type="info" plain icon="el-icon-sort" size="mini" @click="toggleExpandAll">展开/折叠</el-button>
            </el-col>
            <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
        </el-row>

        <el-table v-if="refreshTable" v-loading="loading" :data="typeList" row-key="typeCode"
            :default-expand-all="isExpandAll" :tree-props="{ children: 'children', hasChildren: 'hasChildren' }">
            <el-table-column label="个人评优类别名称" prop="typeName" />
            <el-table-column label="类别代码" align="center" prop="typeCode" />
            <el-table-column label="父级类别代码" align="center" prop="parentCode" />
            <el-table-column label="学年" align="center" prop="stuYearName" />
            <el-table-column label="开始时间" align="center" prop="startTime" width="180">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="结束时间" align="center" prop="endTime" width="180">
                <template slot-scope="scope">
                    <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
                </template>
            </el-table-column>
            <el-table-column label="启用状态" align="center" prop="enableStatus">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.all_status" :value="scope.row.enableStatus" />
                </template>
            </el-table-column>
            <el-table-column label="开始状态" align="center" prop="startStatus">
                <template slot-scope="scope">
                    <dict-tag :options="dict.type.start_status" :value="scope.row.startStatus" />
                </template>
            </el-table-column>
            <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                    <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)"
                        v-hasPermi="['comprehensive:type:edit']">修改</el-button>
                    <el-button size="mini" type="text" icon="el-icon-plus" @click="handleAdd(scope.row)"
                        v-hasPermi="['comprehensive:type:add']">新增</el-button>
                    <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)"
                        v-hasPermi="['comprehensive:type:remove']">删除</el-button>
                </template>
            </el-table-column>
        </el-table>

        <!-- 添加或修改个人评优评先类别对话框 -->
        <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
            <el-form ref="form" :model="form" :rules="rules" label-width="140px">
                <el-form-item label="个人评优类别名称" prop="typeName">
                    <el-input v-model="form.typeName" placeholder="请输入个人评优类别名称" />
                </el-form-item>
                <el-form-item label="类别代码" prop="typeCode">
                    <el-input v-model="form.typeCode" placeholder="请输入类别代码" />
                </el-form-item>
                <el-form-item label="父级类别代码" prop="parentCode">
                    <treeselect v-model="form.parentCode" :options="typeOptions" :normalizer="normalizer"
                        placeholder="请选择父级类别代码" />
                </el-form-item>
                <el-form-item label="学年id" prop="stuYearId">
                    <el-select v-model="form.stuYearId" clearable>
                        <el-option v-for="(v, i) in stuYearList" :key="i" :label="v.stuYearName"
                            :value="v.id"></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item label="唯一申请代码">
                    <el-input v-model="form.onlyCode" placeholder="请输入唯一申请代码" />
                </el-form-item>
                <el-form-item label="开始时间" prop="startTime">
                    <el-date-picker clearable v-model="form.startTime" type="date" value-format="yyyy-MM-dd"
                        placeholder="选择开始时间">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="结束时间" prop="endTime">
                    <el-date-picker clearable v-model="form.endTime" type="date" value-format="yyyy-MM-dd"
                        placeholder="选择结束时间">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="启用状态" prop="enableStatus">
                    <el-radio-group v-model="form.enableStatus">
                        <el-radio v-for="dict in dict.type.all_status" :key="dict.value" :label="dict.value">{{
                            dict.label
                        }}</el-radio>
                    </el-radio-group>
                </el-form-item>
                <el-form-item label="开始状态" prop="startStatus">
                    <el-radio-group v-model="form.startStatus">
                        <el-radio v-for="dict in dict.type.start_status" :key="dict.value" :label="dict.value">{{
                            dict.label
                        }}</el-radio>
                    </el-radio-group>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="submitForm">确 定</el-button>
                <el-button @click="cancel">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import { listType, getType, delType, addType, updateType } from "@/api/stuCQS/good/type";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import { listAllYear } from "@/api/stuCQS/basedata/year";



export default {
    name: "Type",
    dicts: ['all_status', 'start_status'],
    components: {
        Treeselect
    },
    data() {
        return {
            stuYearList: [],

            // 遮罩层
            loading: true,
            // 显示搜索条件
            showSearch: true,
            // 个人评优评先类别表格数据
            typeList: [],
            // 个人评优评先类别树选项
            typeOptions: [],
            // 弹出层标题
            title: "",
            // 是否显示弹出层
            open: false,
            // 是否展开，默认全部展开
            isExpandAll: true,
            // 重新渲染表格状态
            refreshTable: true,
            // 查询参数
            queryParams: {
                typeName: null,
                typeCode: null,
                parentCode: null,
                stuYearId: null,
                startTime: null,
                endTime: null,
                enableStatus: null,
                startStatus: null,
                trees: null,
                status: null,
            },
            // 表单参数
            form: {},
            // 表单校验
            rules: {
                typeCode: [
                    { required: true, message: "类别代码不能为空", trigger: "blur" }
                ],
                delFlag: [
                    { required: true, message: "删除标记不能为空", trigger: "blur" }
                ],
            }
        };
    },
    created() {
        this.getList();
        this.listAllYear();
    },
    methods: {

        async listAllYear() {
            let res = await listAllYear();

            if (res.code == 200) {
                this.stuYearList = [...res.data];
            }
        },

        /** 查询个人评优评先类别列表 */
        getList() {
            this.loading = true;
            listType(this.queryParams).then(response => {
                this.typeList = this.handleTree(response.data, "typeCode", "parentCode");
                this.loading = false;
            });
        },
        /** 转换个人评优评先类别数据结构 */
        normalizer(node) {
            if (node.children && !node.children.length) {
                delete node.children;
            }
            return {
                id: node.typeCode,
                label: node.typeName,
                children: node.children
            };
        },
        /** 查询个人评优评先类别下拉树结构 */
        getTreeselect() {
            listType().then(response => {
                this.typeOptions = [];
                const data = { typeCode: 0, typeName: '顶级节点', children: [] };
                data.children = this.handleTree(response.data, "typeCode", "parentCode");
                this.typeOptions.push(data);
            });
        },
        // 取消按钮
        cancel() {
            this.open = false;
            this.reset();
        },
        // 表单重置
        reset() {
            this.form = {
                id: null,
                typeName: null,
                typeCode: null,
                parentCode: null,
                stuYearId: null,
                startTime: null,
                endTime: null,
                enableStatus: null,
                startStatus: null,
                trees: null,
                status: null,
                delFlag: null,
                createBy: null,
                createTime: null,
                updateBy: null,
                updateTime: null
            };
            this.resetForm("form");
        },
        /** 搜索按钮操作 */
        handleQuery() {
            this.getList();
        },
        /** 重置按钮操作 */
        resetQuery() {
            this.resetForm("queryForm");
            this.handleQuery();
        },
        /** 新增按钮操作 */
        handleAdd(row) {
            this.reset();
            this.getTreeselect();
            if (row != null && row.typeCode) {
                this.form.parentCode = row.typeCode;
            } else {
                this.form.parentCode = 0;
            }
            this.open = true;
            this.title = "添加个人评优评先类别";
        },
        /** 展开/折叠操作 */
        toggleExpandAll() {
            this.refreshTable = false;
            this.isExpandAll = !this.isExpandAll;
            this.$nextTick(() => {
                this.refreshTable = true;
            });
        },
        /** 修改按钮操作 */
        handleUpdate(row) {
            this.reset();
            this.getTreeselect();
            if (row != null) {
                this.form.parentCode = row.typeCode;
            }
            getType(row.id).then(response => {
                this.form = response.data;
                this.open = true;
                this.title = "修改个人评优评先类别";
            });
        },
        /** 提交按钮 */
        submitForm() {
            this.$refs["form"].validate(valid => {
                if (valid) {
                    if (this.form.id != null) {
                        updateType(this.form).then(response => {
                            this.$modal.msgSuccess("修改成功");
                            this.open = false;
                            this.getList();
                        });
                    } else {
                        addType(this.form).then(response => {
                            this.$modal.msgSuccess("新增成功");
                            this.open = false;
                            this.getList();
                        });
                    }
                }
            });
        },
        /** 删除按钮操作 */
        handleDelete(row) {
            this.$modal.confirm('是否确认删除个人评优评先类别编号为"' + row.id + '"的数据项？').then(function () {
                return delType(row.id);
            }).then(() => {
                this.getList();
                this.$modal.msgSuccess("删除成功");
            }).catch(() => { });
        }
    }
};
</script>